package com.zhf.JDBC02;

import com.zhf.utils.JDBCUtils;
import org.junit.Test;

import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Statement;

/**
 * @Author: ZhaoHuanfeng
 * @Date: 2020/9/23 21:23
 */
public class TestDML {
    
    /**
     * 插入数据
     */
    @Test
    public void testInsert() throws SQLException {
        //通过JDBCUtils工具类获取连接
        Connection connection= JDBCUtils.getConnection();
        
        //获取Statement对象
        Statement statement = connection.createStatement();

        //编写SQL
        String sql="insert into jdbc_user values(null,'赵桓锋','123456','2020/09/23')";

        //执行SQL
        int i=statement.executeUpdate(sql);

        //关闭流
        JDBCUtils.close(connection,statement);

    }

    /**
     * 更新操作
     */
    @Test
    public void testUpdate() throws SQLException {
        Connection connection = JDBCUtils.getConnection();

        Statement statement = connection.createStatement();

        String sql="update jdbc_user set username='Jack' where id=1;";
        statement.executeUpdate(sql);
        JDBCUtils.close(connection,statement);
    }

    /**
     * 删除操作
     * @throws SQLException
     */
    @Test
    public void testDelete() throws SQLException {
        Connection connection = JDBCUtils.getConnection();

        Statement statement = connection.createStatement();

        String sql="delete from jdbc_user where id in(1,2);";
        statement.executeUpdate(sql);
        JDBCUtils.close(connection,statement);
    }
}
